<template>
  <div class="search">
    <van-search
      v-model="value"
      placeholder="请输入搜索关键词"
      @keypress.enter="selectMusic" 
      @focus="isFocus"
    />
    <div v-if="isfocus">
      <span class="cancel" @click="cancel">取消</span>
      <div v-if="isSearch">
        <!-- 折叠面板 -->
        <van-collapse v-model="activeName"> </van-collapse>
         <!-- 瀑布流滚动加载，用于展示长列表，当列表即将滚动到底部时，
       会触发事件并加载更多列表项。 -->
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="加载完成"
          @load="onLoad"
        >
         <!-- 单元格 -->
          <van-cell
            @click="playMusic(item)"
            v-for="(item, index) in list"
            :key="index"
            :title="item.songname"
          />
        </van-list>
      </div>
      <div v-else>
        <h4>热搜：</h4>
        <span
          class="hot"
          v-for="(item, index) in hot"
          :key="index"
          @click="selectHot(item)"
          >{{ item.k }}</span
        >
      </div>
    </div>
      </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      value: "",
      isfocus: false,
      isSearch: false,
      isRun: false,
      list: [],
      hot: [],
      musicUrl: "",
      loading: false,
      finished: false,
      activeName: "1",
      isPaused: true,
      iconURlName: "pause"
    };
  },
  created() {
    this.getHot();
  },
  mounted() {

  },
  methods: {
    selectMusic() {
      axios
        .get("/api/search", { params: { key: this.value } })
        .then((res) => {
          // console.log(res.data.data.list);
          this.list = res.data.data.list;
        })
        .catch((err) => {
          console.error(err);
        });

      this.isSearch = true;
    },
    getHot() {
      axios
        .get("/api/search/hot")
        .then((res) => {
          this.hot = res.data.data;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    selectHot(item) {
      this.value = item.k;
      this.selectMusic();
    },
    isFocus() {
      this.isfocus = true;
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // 上拉加载数据
      setTimeout(() => {
        for (let i = 0; i < 20; i++) {
          this.list.push(this.list.length + 1);
        }

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 20) {
          this.finished = true;
        }
      }, 1000);
    },
    playMusic(item) {
      let id = item.songmid;
      axios
        .get("/jsososo/song/urls", {
          params: {
            id: id,
          },
        })
        .then((res) => {
          // console.log(res.data.data[id]);
          // console.log(id);
          this.musicUrl = res.data.data[id];
          this.isRun = true;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    cancel() {
      this.isfocus = false;
      this.value = "";
    },
   
  },
};
</script>

<style scoped>
h4 {
  position: relative;
  right: 40%;
}
.cancel {
  position: absolute;
  top: 2%;
  right: 40px;
  border: 1px solid black;
  border-radius: 10px;
}
.hot {
  display: inline-block;
  border-radius: 5px;
  border: 1px solid beige;
  background-color: azure;
  margin: 5px;
}
.fixStyle {
  /* position: fixed; */
  width: 90%;
  margin: 0 auto;
  background-color: aliceblue;
}

</style>